<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="Hadd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border :data="list">
              <el-table-column label="序号" type="index" width="80" />
              <el-table-column label="角色名称" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success" @click="HclickPerm(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="Edit(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="del(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="pagingData.page"
                :page-sizes="[2, 4, 6, 8]"
                :page-size="pagingData.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 新增弹框 -->
      <el-dialog
        :title="isEdit ?'添加角色':'编辑角色'"
        :visible.sync="showDialog"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        @close="$refs.roleForm.resetFields()"
      >

        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="showDialog = false">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row>

      </el-dialog>
      <!-- 角色新增权限的弹层 -->
      <el-dialog
        title="分配权限"
        :visible.sync="perShowDialog"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
      >
        <settingsPermission v-if="perShowDialog" :row-id="rowId" @close="perShowDialog=false" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getRole, addRole, getRoleDetails, putRole, delRole } from '@/api/settings.js'
import settingsPermission from './settingsPermission'
export default {
  components: { settingsPermission },
  data() {
    return {
      rowId: null, // 保存当前被点击角色的id
      perShowDialog: false, // 控制角色权限弹层
      showDialog: false, // 控制新增弹层呢
      isEdit: null, // 动态控制弹层的状态
      // 表单数据
      roleForm: {
        name: '',
        description: ''
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色详情', trigger: 'blur' }
        ]
      },
      // 分页数据
      pagingData: {
        page: 1, // 当前页码值
        pagesize: 2 // 每页显示几条数据
      },
      total: null, // 保存获取到列表的总条数一共几条
      list: [] // 保存获取到的角色列表数据
    }
  },
  created() {
    this.loadRole()
  },
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pagingData.page = 1
      this.pagingData.pagesize = val
      this.loadRole()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.pagingData.page = val
      this.loadRole()
    },
    // 获取角色列表数据
    async  loadRole() {
      try {
        const res = await getRole(this.pagingData)
        // console.log(res)
        if (res.code !== 10000) return
        // 保存数据
        this.total = res.data.total
        this.list = res.data.rows
      } catch (error) {
        console.log('获取角色error', error)
      }
    },
    // 新增状态设置
    Hadd() {
      this.isEdit = false // 修改弹层状态
      this.showDialog = true
    },
    // 编辑状态
    Edit(id) {
      this.isEdit = true // 修改弹层状态
      this.showDialog = true
      this.loadRoleDetails(id)
    },
    // 根据id获取对应角色信息填充到表单内
    async loadRoleDetails(id) {
      try {
        const res = await getRoleDetails(id)
        // console.log(res)
        if (res.code !== 10000) return
        this.roleForm = res.data // 保存到表单数据 进行数据的回显
      } catch (error) {
        console.log('根据id查询角色详情error', error)
      }
    },
    // 新增角色
    async add() {
      try {
        const res = await addRole(this.roleForm)
        // console.log(res)
        if (res.code !== 10000) return
        this.$message.success(res.message)
        this.loadRole()
        this.total++
        // 页码值   =  总条数/每页几条
        this.pagingData.page = Math.ceil(this.total / this.pagingData.pagesize)
        this.loadRole()
        this.showDialog = false
      } catch (error) {
        console.log('新增角色error', error)
      }
    },
    // 修改角色
    async dit() {
      try {
        const res = await putRole(this.roleForm)
        // console.log(res)
        if (res.code !== 10000) return
        this.$message.success(res.message)
        console.log(res)
        this.loadRole()
        this.showDialog = false
      } catch (error) {
        console.log('编辑角色')
      }
    },
    // 根据id 删除对应的角色
    del(id) {
      this.$confirm('你点击了删除, 是否继续', '提示').then(async() => {
        try {
          const res = await delRole(id)
          if (res.code !== 10000) return
          // console.log(res)
          this.$message.success(res.message)
          // 删除后判断当前页面是否还有数据没有数据就请求上一页的数据进行渲染
          if (this.list.length === 1) {
            this.pagingData.page--
            // 页码值最小是  1
            // 判断当前页码小于或等于0的时候
            if (this.pagingData.page <= 0) {
              // 让页码值 = 1
              this.pagingData.page = 1
            }
          }
          this.loadRole()
        } catch (error) {
          console.log('根据id删除对应角色', error)
        }
      }).catch(err => console.log(err))
    },
    // 点击了确定
    hSubmit() {
      this.$refs.roleForm.validate((valid) => {
        if (!valid) return
        // 三元判断弹层状态
        this.isEdit ? this.dit() : this.add()
      })
    },
    // 点击了分配权限 接收该被点击角色的id
    HclickPerm(id) {
      this.rowId = id
      this.perShowDialog = true
    }
  }
}
</script>
